import numpy as np
import random
import time

def get_track7(distance):
        """
        根据偏移量和手动操作模拟计算移动轨迹
        :param distance: 偏移量
        :return: 移动轨迹
        """
        # 移动轨迹
        tracks = []
        # 当前位移
        current = 0
        # 减速阈值
        mid = distance * 3 / 5
        # 时间间隔
        t = 0.08
        # 初始速度
        v = 0

        while current < distance:
            if current < mid:
                a = random.uniform(2, 5)
            else:
                a = -(random.uniform(12.5, 13.5))
            v0 = v
            v = v0 + a * t
            x = v0 * t + 1 / 2 * a * t * t
            current += x

            if 0.6 < current - distance < 1:
                x = x - 0.53
                tracks.append(round(x, 2))

            elif 1 < current - distance < 1.5:
                x = x - 1.4
                tracks.append(round(x, 2))
            elif 1.5 < current - distance < 3:
                x = x - 1.8
                tracks.append(round(x, 2))

            else:
                tracks.append(round(x, 2))

        print(tracks, sum(tracks),len(tracks))
        return tracks   
 
def get_tracks(distance):
    trace = []
    first_time = int(time.time()*1000)
    trace.append(["1137","342",first_time])

    # 设置初始位置、初始速度、时间间隔
    start, v0, t = 1166, 1.25, random.randint(7, 11)/10
    first_distance = start + int(distance * 3 / 5)
    last_distance = start + int(distance *4 / 5)
    # 当尚未移动到终点时
    count = 1
    while start < first_distance:
        distance = distance - v0 * t
        move = start + v0 * t
        start = start + v0 * t
        if count == 1:
            trace.append([str(round(move)), "383", first_time])
            count = count + 1 
        else:
            trace.append([str(round(move)), "383", int(first_time + t * 10)])
            first_time = first_time + t * 10
    while first_distance<=start < last_distance:
        v0 = 1.3
        t = random.randint(13, 16)/10
        distance = distance - v0 * t
        move = start + v0 * t
        start = start + v0 * t
        trace.append([str(round(move)), "384", int(first_time + t * 10)])
        first_time = first_time + t * 10
    while start>=last_distance:
        v0 = 1.25
        t = random.randint(7, 11)/10
        if distance<=0:
            print(distance)
            print(start + distance)
            trace.append([str(round(start + distance)), "385", int(trace[-1][2] + random.randint(2000,10000))])
            print(trace[-1])
            if trace[-2][0] != trace[-1][0]:
                trace[-2][0] = trace[-1][0]
            return trace
        distance = distance - v0 * t
        move = start + v0 * t
        start = start + v0 * t
        trace.append([str(round(move)),"385",int(first_time+ t * 10)])
        first_time = first_time + t * 10
    trace.append([str(start + distance), "385", int(trace[-1][2] + random.randint(2000,10000))])
    if trace[-2][0] != trace[-1][0]:
        trace[-2][0] = trace[-1][0]
    return trace


if __name__ == "__main__":
    z = get_tracks(111)
    print(z)
    print(len(z))